package wangjg.coco.widgets;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

public class FileInput extends Composite {
	public static final int FILE = 0;
	public static final int DIR = 1;
	
	private Label	label = null;
	private Text	text = null;
	private Button	button = null;
	private int 	type = FILE;
	private boolean multi = false;
	
	private FileDialog fileDialog ;
	private DirectoryDialog folderDialog ;

	private Listener selectListener;
	
	private Listener changeListener;
	
	private Listener cg_listener = new Listener(){
		private boolean textChanged = false;
		public void handleEvent(Event event) {
			System.out.println(event);
			if(event.type==SWT.CHANGED){
				textChanged = true;
			}else if(event.type==SWT.FocusOut){
				if(textChanged && changeListener!=null){
					onTextChange();
				}
			}
		}};
	
	public FileInput(Composite parent, int style) {
		super(parent, style);
		this.setLayout(this);
		this.init();
	}
	
	private void init(){
		label = new Label(this, SWT.NONE);
		text = new Text(this, SWT.BORDER);
		text.addListener(SWT.CHANGED, cg_listener);
		text.addListener(SWT.FocusOut, cg_listener);
		
		this.setGridData(text, 1, 1);
		button = new Button(this, SWT.NONE);
		button.setText("浏览..");
		
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				String path = null;
				if(type==DIR){
					path = selectDirectory(text.getText());
				}else if(type==FILE){
					path = selectFile(text.getText());
				}
				if(path==null){
					return;
				}
				boolean changed = true;// !path.equals(text.getText());
				text.setText(path);
				if(changed){
					onTextChange();
				}
			}
		});
	}
	
	private void onTextChange(){
		Event ce = new Event();
		ce.type = SWT.CHANGED;
		ce.data = text.getText();
		if(changeListener!=null){
			changeListener.handleEvent(ce);
		}
	}
	
	private void setLayout(Composite composite){
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.makeColumnsEqualWidth = false;
		composite.setLayout(gridLayout);
	}
	
	private void setGridData(Control control, Integer rowSpan, Integer colSpan){
		GridData gd = new GridData();
		gd.horizontalSpan = rowSpan;
		if(colSpan!=null){
			gd.verticalSpan = colSpan;
		}
		gd.horizontalAlignment = SWT.FILL;
		gd.grabExcessHorizontalSpace = true;//?
		control.setLayoutData(gd);
	}
	
	private String selectDirectory(String path){
		int style = SWT.NONE;
		if(this.multi){
			style = SWT.MULTI;
		}
		folderDialog = new DirectoryDialog(this.getShell(), style);
		folderDialog.setText("选择目录");
		folderDialog.setFilterPath(path);
		String selectedDir = folderDialog.open();
		return selectedDir;
	}
	
	private String selectFile(String path){
		int style = SWT.NONE;
		if(this.multi){
			style = SWT.MULTI;
		}
		fileDialog = new FileDialog(this.getShell(), style);
		fileDialog.setText("选择文件");
		fileDialog.setFilterPath(path);
		String selectedDir = fileDialog.open();
		if(this.multi){
			return getParent(selectedDir);
		}else{
			return selectedDir;
		}
	}
	
	private String getParent(String path){
		if(path==null){
			return path;
		}
		return new File(path).getParent();
	}
	
	public String[] getFileNames(){
        return fileDialog.getFileNames();
	}

	public Label getLabel() {
		return label;
	}

	public Text getText() {
		return text;
	}

	public Button getButton() {
		return button;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public boolean isMulti() {
		return multi;
	}

	public void setMulti(boolean multi) {
		this.multi = multi;
	}

	public void setChangeListener(Listener changeListener) {
		this.changeListener = changeListener;
	}
	
	public String getValue(){
		return this.text.getText();
	}
	
	public void setValue(String text){
		this.text.setText(text);
	}
}
